package com.it.travel.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.travel.dao.StoryMapper;
import com.it.travel.pojo.Story;
import com.it.travel.service.admin.AdminStoryService;
import com.it.travel.vo.Params.PageParams;
import com.it.travel.vo.Params.admin.AdminStoryAddParams;
import com.it.travel.vo.Params.admin.AdminStoryUpdateParams;
import com.it.travel.vo.Result;
import com.it.travel.vo.admin.AdminStoryVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class AdminStoryServiceImpl implements AdminStoryService {
    @Resource
    private StoryMapper storyMapper;

    @Override
    public Result<?> getStoryInfoPage(PageParams pageParams) {
        LambdaQueryWrapper<Story> wrapper = Wrappers.lambdaQuery();
        Page<Story> page = new Page<>(pageParams.getIndex(), pageParams.getSize());
        if (StringUtils.isNotBlank(pageParams.getKeywords())) {  // 不为空就加上条件
            wrapper.like(Story::getTitle, pageParams.getKeywords());
        }
        Page<Story> storyPage = storyMapper.selectPage(page, wrapper);
        List<Story> records = storyPage.getRecords();
        List<AdminStoryVO> list = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();
        long total = storyPage.getTotal();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Story story : records) {
            String description = story.getDescription();
            String shortStr = description;
            if (description.length() >= 12) {
                shortStr = shortStr.substring(0, 11) + "......";
            }
            AdminStoryVO adminStoryVO = new AdminStoryVO();
            String format = simpleDateFormat.format(new Date(story.getCreateTime()));
            BeanUtil.copyProperties(story, adminStoryVO);
            adminStoryVO.setFormatTime(format);
            adminStoryVO.setDescription(shortStr);
            list.add(adminStoryVO);
        }
        map.put("storyList", list);
        map.put("total", total);
        return Result.success(map, "获取成功");
    }

    @Override
    public Result<?> save(AdminStoryAddParams adminStoryAddParams) {
        Story story = new Story();
        BeanUtil.copyProperties(adminStoryAddParams, story);
        story.setCreateTime(System.currentTimeMillis());
        story.setUpdateTime(System.currentTimeMillis());
        int insert = storyMapper.insert(story);
        if (insert > 0) {
            return Result.success(null, "新增成功");
        }
        return Result.error(null, "新增失败");
    }

    @Override
    public Result<?> delete(Integer id) {
        int i = storyMapper.deleteById(id);
        if (i > 0) {
            return Result.success(null, "删除成功");
        }
        return Result.error(null, "删除成功");
    }

    @Override
    public Result<?> deletebatch(List<Integer> ids) {
        int i = storyMapper.deleteBatchIds(ids);
        if (i > 0) {
            return Result.success(null, "删除成功");
        }
        return Result.error(null, "删除成功");

    }

    @Override
    public Result<?> update(AdminStoryUpdateParams adminStoryUpdateParams) {
        Story story = new Story();
        BeanUtil.copyProperties(adminStoryUpdateParams, story);
        story.setUpdateTime(System.currentTimeMillis());
        int i = storyMapper.updateById(story);
        if (i > 0) {
            return Result.success(null, "修改成功");
        }
        return Result.error(null, "修改失败");
    }

    @Override
    public Result<?> findStoryDetail(Integer id) {
        Story story = storyMapper.selectById(id);
        return Result.success(story, "获取数据成功");

    }


//    @Override
//    public Result<?> updateStoryDetail(AdminStoryUpdateParams adminStoryUpdateParams) {
//        Story story = new Story();
//        BeanUtil.copyProperties(adminStoryUpdateParams,story);
//        story.setUpdateTime(System.currentTimeMillis());
//        int i = storyMapper.updateById(story);
//
//
//    }
}
